$(function(){
	chart_index.run();
});

var chart_index = {
		run: function(){
			this._init_();
			this._start_();			
			this._events_();
		},
		
		_init_: function(){
			this.contextPath = $("#contextPath").val();			
		},
		
		_start_: function(){				
			$("#top_browse_daily_div").data("videoLoaded", true);
			$("#top_artist_daily_div").data("artistLoaded", true);
		},
		
		_events_: function(){
			$("#top_video_div, #top_artist_div").tabs({
				hoverClass: 'curr',
				onswitch: function($nav, $tab, $this){					
					chart_index._get_charts($nav, $tab, $this);					
				}
			});
		},
		
		_get_charts: function($nav, $tab, $this){
			var cacheKey = null;
			if($this.attr("type") == 'video'){
				cacheKey = "videoLoaded";
			}else{
				cacheKey = "artistLoaded";
			}
			
			if($tab.data(cacheKey)){
				return;
			}
			
			var type = $nav.attr("type");
			var period = $nav.attr("period");
			var url = this.contextPath + "/music/chart/" + type + "/" + period;
			$.get(url, function(data){
				$tab.html(data);
				$tab.data(cacheKey, true);
			});
		}
};
